<?php

declare(strict_types=1);

namespace App\Controller\AdminApi;

use App\Exception\XZXValidationException;
use App\Service\TradeOrderReviewService;
use App\Service\TradeOrderThawService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Logger\LoggerFactory;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use App\Middleware\AdminApi\AdminJWTAuthMiddleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use App\Middleware\AdminApi\NodeAccessMiddleware;

/**
 * Class TradeOrderThawController
 *
 * @package App\Controller
 * @Controller(prefix="/admin/api/tradeOrderThaw")
 */
class TradeOrderThawController extends AdminBaseController {
    /**
     * @Inject()
     * @var ValidatorFactoryInterface
     */
    protected $validationFactory;

    /**
     * @Inject()
     * @var TradeOrderThawService
     */
    protected $TradeOrderThawService;

    /**
     * @Inject()
     * @var TradeOrderReviewService
     */
    protected $TradeOrderReviewService;

    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;

    public function __construct (LoggerFactory $loggerFactory) {
        // 第一个参数对应日志的 name, 第二个参数对应 config/autoload/logger.php 内的 key
        $this->logger = $loggerFactory->get('log', 'default');
    }

    /**
     * 添加
     * @PostMapping(path = "add")
     * @Middlewares({
     *     @Middleware(AdminJWTAuthMiddleware::class),
     *     @Middleware(NodeAccessMiddleware::class)
     * }))
     */
    public function add (RequestInterface $request) {

        $validator = $this->validationFactory->make($request->all(), [
            'tradeNo'     => 'required',
            'transReason' => 'required',
        ]);

        if ($validator->fails()) {
            // Handle exception
            $errorMessage = $validator->errors()->first();
            throw new XZXValidationException($errorMessage);
        }

        $param                = $request->all();
        $ext                  = $this->request->getAttribute("ext");
        $param['isSuperRole'] = $ext['isSuperRole'];
        if (!$param['isSuperRole']) {
            $param['mchIdList'] = $ext['mchIdList'];
        }
        $param['source']   = 2;
        $param['createBy'] = $ext['createBy'];
        return $this->TradeOrderThawService->add($param);
    }

    /**
     * 初审
     * @PostMapping(path = "handleFirstReview")
     * @Middlewares({
     *     @Middleware(AdminJWTAuthMiddleware::class),
     *     @Middleware(NodeAccessMiddleware::class)
     * })
     * @throws \Throwable
     */
    public function handleFirstReview (RequestInterface $request) {

        $validator = $this->validationFactory->make($request->all(), [
            'tradeNoList' => 'required|array',
            'isApprove'   => 'required',
        ]);

        if ($validator->fails()) {
            // Handle exception
            $errorMessage = $validator->errors()->first();
            throw new XZXValidationException($errorMessage);
        }

        $ext    = $this->request->getAttribute("ext");
        $params = [
            'tradeNoList' => $request->post('tradeNoList'),
            'isApprove'   => !!$request->post('isApprove'),
            'remark'      => $request->post('remark', ''),
            'reviewLevel' => 1,
        ];

        $params['createBy'] = $ext['createBy'];

        $params['source']      = 2;
        $params['isSuperRole'] = $ext['isSuperRole'];
        if (!$params['isSuperRole']) {
            $params['mchIdList'] = $ext['mchIdList'];
        }

        return $this->TradeOrderReviewService->handleThawReview($params);
    }

    /**
     * 复审
     * @PostMapping(path = "handleSecondReview")
     * @Middlewares({
     *     @Middleware(AdminJWTAuthMiddleware::class),
     *     @Middleware(NodeAccessMiddleware::class)
     * })
     * @throws \Throwable
     */
    public function handleSecondReview (RequestInterface $request) {

        $validator = $this->validationFactory->make($request->all(), [
            'tradeNoList' => 'required',
            'isApprove'   => 'required',
        ]);

        if ($validator->fails()) {
            // Handle exception
            $errorMessage = $validator->errors()->first();
            throw new XZXValidationException($errorMessage);
        }

        $ext    = $this->request->getAttribute("ext");
        $params = [
            'tradeNoList' => $request->post('tradeNoList'),
            'isApprove'   => !!$request->post('isApprove'),
            'remark'      => $request->post('remark', ''),
            'reviewLevel' => 2,
        ];

        $params['createBy'] = $ext['createBy'];

        $params['source']      = 2;
        $params['isSuperRole'] = $ext['isSuperRole'];
        if (!$params['isSuperRole']) {
            $params['mchIdList'] = $ext['mchIdList'];
        }

        return $this->TradeOrderReviewService->handleThawReview($params);
    }

}
